package com.miaoxun.common.rand;

import java.util.Random;

public class Rand {

    /// <summary>
    /// 随机系数
    /// </summary>
    public static int _RandIndex = 9999;


    /// <summary>
    /// 数字随机数
    /// </summary>
    /// <param name="length">生成长度</param>
    /// <returns>返回指定长度的数字随机串</returns>
    public static String RndNum(int length) {
        if (_RandIndex >= 1000000) _RandIndex = 1;
        _RandIndex = GetRandIndex();
        char[] arrChar = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuilder num = new StringBuilder();
        Random rnd = new Random(System.currentTimeMillis() + _RandIndex);
        for (int i = 0; i < length; i++) {
            num.append(String.valueOf(arrChar[rnd.nextInt(9)]));
        }
        return num.toString();
    }

    private static int GetRandIndex() {
        char[] arrChar = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuilder num = new StringBuilder();
        Random rnd = new Random(System.currentTimeMillis() + _RandIndex);
        for (int i = 0; i < 4; i++) {
            num.append(String.valueOf(arrChar[rnd.nextInt(9)]));
        }
        return Integer.parseInt(num.toString());

    }
}
